package com.timemanager.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.timemanager.R;
import com.timemanager.adapter.EventAndEventTimeAdapter;
import com.timemanager.db.TimeManagerDB;
import com.timemanager.model.EventAndEventTime;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class HistoryEventTimeActivity2 extends AppCompatActivity {

    private TextView titleText;
    private ListView listView;
    private List<EventAndEventTime> dataList = new ArrayList<EventAndEventTime>();
    private List<EventAndEventTime> eventAndEventTimes = new ArrayList<EventAndEventTime>();
    private TimeManagerDB timeManagerDB;
    private String fromDate;
    private String toDate;
    private EventAndEventTimeAdapter adapter;
    private EventAndEventTime selectedEventAndEventTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_event_time2);
        fromDate = getIntent().getStringExtra("from_date");
        toDate = getIntent().getStringExtra("to_date");
        timeManagerDB = TimeManagerDB.getInstance(HistoryEventTimeActivity2.this);
        titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText("活动详情清单");
        listView = (ListView) findViewById(R.id.list_view);
        adapter = new EventAndEventTimeAdapter(HistoryEventTimeActivity2.this,
                R.layout.event_and_event_time_item,dataList);
        listView.setAdapter(adapter);
        //显示列表
        queryEventAndEventTime();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedEventAndEventTime = dataList.get(position);
                showItemDialog(selectedEventAndEventTime.getId());
            }
        });
    }

    private void queryEventAndEventTime() {
        eventAndEventTimes = timeManagerDB.loadEventAndEventTime(fromDate, toDate);
        dataList.clear();
        dataList.addAll(eventAndEventTimes);
        adapter.notifyDataSetChanged();
        listView.setSelection(0);
    }

    private void showItemDialog(final int selectedId) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(HistoryEventTimeActivity2.this);
        final String[] operations = {"删除"};
        dialogBuilder.setItems(operations, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (operations[which].equals("删除")) {
                    if (timeManagerDB.deleteEventTime(selectedId)) {
                        Toast.makeText(HistoryEventTimeActivity2.this, "删除成功", Toast.LENGTH_SHORT).show();
                        queryEventAndEventTime();
                    } else {
                        Toast.makeText(HistoryEventTimeActivity2.this, "删除失败，未知原因", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        dialogBuilder.show();
    }
}
